compareTo() method
The Java string compareTo() method will allow you to compare the provided string with the current string lexicographically. The output may be positive, negative or zero. The Unicode value of each character will be compared to both the strings. There will be three conditions-
- if s1 > s2, it returns positive number
- if s1 < s2, it returns negative number
- if s1 == s2, it returns 0
Where s1 and s2 are the two strings.
Syntax-
Example-
Output-
Example with empty strings-
Output-